//
//  AnimationsViewController.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AnimationsViewController.h"
#import "AnimansTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface AnimationsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UIView *headerView;
@property (nonatomic,retain)UIView *aninma;

@property (nonatomic,retain)UITableView *tableV;

@end

@implementation AnimationsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addTableView];
    [self addHeaderView];

}

- (void)addTableView
{
    
    
    
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50 * OffHeight) style:UITableViewStylePlain];
    _tableV.delegate = self;
    _tableV.dataSource = self;
    _tableV.tableHeaderView = self.aninma;
    _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
  
    
    
    
    UIImageView *jjj = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    
    
    
    [jjj sd_setImageWithURL:self.backUrl];
    [_tableV setBackgroundView:jjj];
    [self.view addSubview:_tableV];
 
    
    UIBlurEffect *ef = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effV = [[UIVisualEffectView alloc]initWithEffect:ef];
    effV.alpha = 0.5;
    effV.frame = [UIScreen mainScreen].bounds;
    [jjj addSubview:effV];
    

    [jjj release];
    [_tableV release];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"e"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"e"];
            
            self.aninma = [[UIView alloc]initWithFrame:CGRectMake(0,62 * OffWidth, self.view.frame.size.width, 270 * OffHeight)];
            self.aninma.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.3];
            [cell addSubview:_aninma];
         for (int i = 0; i < 6; i ++) {
             
                //高温度
             
                UILabel *tempHigh = [[UILabel alloc]init];
                tempHigh.textColor = [UIColor whiteColor];
             tempHigh.font = [UIFont systemFontOfSize:17 * OffWidth];
                tempHigh.frame = CGRectMake(i * 60 + 25 * OffWidth, 410 * OffHeight - [[[[self.arr objectAtIndex:i] objectForKey:@"temp"] objectForKey:@"height"] integerValue] * 10, 30 * OffWidth, 30 * OffHeight) ;
                
                tempHigh.text  = [[[[self.arr objectAtIndex:i] objectForKey:@"temp"] objectForKey:@"height"] stringValue];
                [cell addSubview:tempHigh];
                
                //低温度
                UILabel *tempLow = [[UILabel alloc]init];
                tempLow.font = [UIFont systemFontOfSize:17 * OffWidth];

                tempLow.textColor = [UIColor whiteColor];
                tempLow.frame = CGRectMake(i * 60 + 25 * OffWidth, 430 * OffHeight - [[[[self.arr objectAtIndex:i] objectForKey:@"temp"] objectForKey:@"low"] integerValue] * 10, 30 * OffWidth, 30 * OffHeight) ;
                tempLow.text  = [[[[self.arr objectAtIndex:i] objectForKey:@"temp"] objectForKey:@"low"] stringValue];
                [cell addSubview:tempLow];
                //高 符号
                
                
                UILabel  *highDu  = [[UILabel alloc]init];
                highDu.text = @"。";
                highDu.textColor = [UIColor whiteColor];
                highDu.frame =  CGRectMake(i * 60 + 43 * OffWidth, 405 * OffHeight - [[[[self.arr objectAtIndex:i] objectForKey:@"temp"] objectForKey:@"height"] integerValue] * 10, 30 * OffWidth, 30 * OffHeight) ;
                [cell addSubview:highDu];
                //低  符号
                UILabel *lowDu = [[UILabel alloc]init];
                lowDu.text = @"。";
                lowDu.textColor = [UIColor whiteColor];
                lowDu.frame = CGRectMake(i * 60 + 43 * OffWidth, 420 * OffHeight - [[[[self.arr objectAtIndex:i] objectForKey:@"temp"] objectForKey:@"low"] integerValue] * 10, 30 * OffWidth, 30 * OffHeight) ;
                
                [cell addSubview:lowDu];
            }
            
            
        }
      
        
        
        [self drawLinepoint1y:350 * OffHeight - [[[[self.arr objectAtIndex:0] objectForKey:@"temp"] objectForKey:@"height"] integerValue] * 9 point2y:350 * OffHeight -  [[[[self.arr objectAtIndex:1] objectForKey:@"temp"] objectForKey:@"height"] integerValue] * 9 point3y:350 * OffHeight - [[[[self.arr objectAtIndex:2] objectForKey:@"temp"] objectForKey:@"height"] integerValue] * 9 point4y:350 * OffHeight  - [[[[self.arr objectAtIndex:3] objectForKey:@"temp"] objectForKey:@"height"] integerValue] * 9 point5y:350 * OffHeight - [[[[self.arr objectAtIndex:4] objectForKey:@"temp"] objectForKey:@"height"] integerValue] * 9 point6y:350 * OffHeight -[[[[self.arr objectAtIndex:5] objectForKey:@"temp"] objectForKey:@"height"] integerValue]* 9] ;
        
        
        
        
        [self drawLinepoint1y:350 * OffHeight - [[[[self.arr objectAtIndex:0] objectForKey:@"temp"] objectForKey:@"low"] integerValue] * 9 point2y:350 * OffHeight -  [[[[self.arr objectAtIndex:1] objectForKey:@"temp"] objectForKey:@"low"] integerValue] * 9 point3y:350 * OffHeight - [[[[self.arr objectAtIndex:2] objectForKey:@"temp"] objectForKey:@"low"] integerValue] * 9 point4y:350 * OffHeight  - [[[[self.arr objectAtIndex:3] objectForKey:@"temp"] objectForKey:@"low"] integerValue] * 9 point5y:350 * OffHeight - [[[[self.arr objectAtIndex:4] objectForKey:@"temp"] objectForKey:@"low"] integerValue] * 9 point6y:350 * OffHeight -[[[[self.arr objectAtIndex:5] objectForKey:@"temp"] objectForKey:@"low"] integerValue]* 9] ;
        
        
        
        
     
        for ( int i = 0; i < 6; i ++) {
            
            
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 6 * i , 50 * OffWidth, self.view.frame.size.width / 6, 50 * OffHeight)];
            lable.text  =  [[self.arr objectAtIndex:i] objectForKey:@"dateName"];
            lable.font = [UIFont boldSystemFontOfSize:13 * OffWidth];
            lable.textAlignment = 1
            ;
            lable.textColor = [UIColor whiteColor];
            [cell addSubview:lable];
            [lable release];
            
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
    }else{
        
        AnimansTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"e"];
        if (cell == nil) {
            cell = [[AnimansTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"e"];
          
        }
        
        
        cell.weak.text = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"dateName" ];
                          
        cell.high.text = [[[[self.arr objectAtIndex:indexPath.row] objectForKey:@"temp"] objectForKey:@"height"] stringValue];
        cell.low.text = [[[[self.arr objectAtIndex:indexPath.row] objectForKey:@"temp"] objectForKey:@"low"] stringValue];
        cell.weather.text = [[[self.arr objectAtIndex:indexPath.row] objectForKey:@"climate"] objectForKey:@"name"];
        cell.date.text = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"date"];
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        return cell;
        
        
    }
    

    

  
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    if (section == 0) {
        return 1;
    }else{
        return 6;
    }
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return self.view.frame.size.height / 2;
    }else{
        return (self.view.frame.size.height  - 382 * OffHeight ) / 6;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (void)addHeaderView
{
    
    self.headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60 * OffHeight)];
    _headerView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    [self.view addSubview:_headerView];
    [_headerView release];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 20 * OffHeight, 50 * OffWidth, 50 * OffHeight);
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];

    
    
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 25 * OffWidth , 20 * OffHeight, 100 * OffWidth, 50 * OffHeight)];
    name.text = self.QSName;
    name.textColor = [UIColor whiteColor];
    [self.view addSubview:name];
    [name release];
    
    
}


- (void)drawLinepoint1y:(CGFloat)point1y point2y:(CGFloat)point2y point3y:(CGFloat)point3y point4y:(CGFloat)point4y point5y:(CGFloat)point5y point6y:(CGFloat)point6y

{
    
    //view是曲线的背景view
//    self.aninma = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
//    self.aninma.backgroundColor = [UIColor blackColor];
//    [self.view addSubview:self.aninma];
//    [self.aninma release];
    
    
    //第一、UIBezierPath绘制线段
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    //四个点
    CGPoint point  = CGPointMake(self.view.frame.size.width / 12,  point1y);
    CGPoint point1 = CGPointMake(self.view.frame.size.width / 6 + self.view.frame.size.width / 12, point2y);
    CGPoint point2 = CGPointMake(self.view.frame.size.width / 6 * 2 + self.view.frame.size.width / 12 , point3y);
    CGPoint point3 = CGPointMake(self.view.frame.size.width / 6 * 3 + self.view.frame.size.width / 12, point4y);
    CGPoint point4 = CGPointMake(self.view.frame.size.width / 6 * 4  + self.view.frame.size.width / 12, point5y);
    CGPoint point5 = CGPointMake(self.view.frame.size.width / 6 * 5 + self.view.frame.size.width / 12, point6y);
    

    
    
    NSArray *arr = [NSArray arrayWithObjects:[NSValue valueWithCGPoint:point],[NSValue valueWithCGPoint:point1],[NSValue valueWithCGPoint:point2],[NSValue valueWithCGPoint:point3],[NSValue valueWithCGPoint:point4],[NSValue valueWithCGPoint:point5], nil];
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, arr.count)];
    
    [arr enumerateObjectsAtIndexes:set options:0 usingBlock:^(NSValue *pointValue, NSUInteger idx, BOOL *stop){
        
        CGPoint point = [pointValue CGPointValue];
        [path addLineToPoint:point];
        
        //（一）rect折线画法
        CGRect rect;
        rect.origin.x = point.x - 1.5 * OffWidth;
        rect.origin.y = point.y - 1.5 * OffHeight;
        rect.size.width = 5 * OffWidth;
        rect.size.height = 5 * OffHeight;
        
        //（二）rect射线画法
//       CGRect rect = CGRectMake(10, 10, 1, 1);
        
        UIBezierPath *arc= [UIBezierPath bezierPathWithOvalInRect:rect];
        [path appendPath:arc];
    }];
    //第三、UIBezierPath和CAShapeLayer关联
    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.frame = CGRectMake(0, 0, 320 * OffWidth, 400 * OffHeight);
    
 
    lineLayer.fillColor = [UIColor whiteColor].CGColor;
    lineLayer.path = path.CGPath;
    lineLayer.strokeColor = [UIColor whiteColor].CGColor;
    [self.aninma.layer addSublayer:lineLayer];
  
 
    
    //以下代码为附加的
    //（一）像一个幕布一样拉开，显得有动画
    CGFloat l = (self.view.frame.size.height - 382 * OffWidth)  / 6;
    CGFloat o = self.view.frame.size.height / 2;
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0,62 * OffWidth, self.view.frame.size.width, 270 * OffHeight)];
    view1.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view1];

    [UIView animateWithDuration:1 animations:^{

        view1.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.01];
        
    } completion:^(BOOL finished) {
        [view1 release];
        [view1 removeFromSuperview];
     
        
    }];
    UIView *view2 = [[UIView alloc]initWithFrame:CGRectMake(0,o, self.view.frame.size.width, l)];
    view2.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:view2];
    
    [UIView animateWithDuration:1 animations:^{
        
        view2.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.01];
        
    } completion:^(BOOL finished) {
        [view2 release];
        [view2 removeFromSuperview];
        
        
    }];
    UIView *view3 = [[UIView alloc]initWithFrame:CGRectMake(0,o + l, self.view.frame.size.width, l)];
    view3.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:view3];
    
    [UIView animateWithDuration:2 animations:^{
        
        view3.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.01];
        
    } completion:^(BOOL finished) {
        [view3 release];
        [view3 removeFromSuperview];
        
        
    }];

    UIView *view4 = [[UIView alloc]initWithFrame:CGRectMake(0,o + 2 * l, self.view.frame.size.width, l)];
    view4.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:view4];
    
    [UIView animateWithDuration:3 animations:^{
        
        view4.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.01];
        
    } completion:^(BOOL finished) {
        [view4 release];
        [view4 removeFromSuperview];
        
        
    }];

    UIView *view5 = [[UIView alloc]initWithFrame:CGRectMake(0,o + 3 * l, self.view.frame.size.width, l)];
    view5.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:view5];
    
    [UIView animateWithDuration:4 animations:^{
        
        view5.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.01];
        
    } completion:^(BOOL finished) {
        [view5 release];
        [view5 removeFromSuperview];
        
        
    }];

    UIView *view6 = [[UIView alloc]initWithFrame:CGRectMake(0,o + 4 * l, self.view.frame.size.width, l)];
    view6.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:view6];

    [UIView animateWithDuration:5 animations:^{
        
        view6.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.01];
        
    } completion:^(BOOL finished) {
        [view6 release];
        [view6 removeFromSuperview];
        
        
    }];
    UIView *view7 = [[UIView alloc]initWithFrame:CGRectMake(0,o + 5 * l, self.view.frame.size.width, l)];
    view7.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:view7];
    
    [UIView animateWithDuration:5.5 animations:^{
        
        view7.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.01];
        
    } completion:^(BOOL finished) {
        [view7 release];
        [view7 removeFromSuperview];
        
        
    }];


    

}

- (void)back:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//- (void)DrawLineWithView:(UIView *)view
//{
//    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
//    [shapeLayer setBounds:view.bounds];
//    [shapeLayer setPosition:view.center];
//    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
//    
//    // 设置虚线颜色为blackColor
//    [shapeLayer setStrokeColor:[[UIColor greenColor] CGColor]];
//    //    [shapeLayer setStrokeColor:[[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0f] CGColor]];
//    
//    // 3.0f设置虚线的宽度
//    [shapeLayer setLineWidth:1.0f];
//    [shapeLayer setLineJoin:kCALineJoinMiter];
//    
//    // 3=线的宽度 1=每条线的间距
//    [shapeLayer setLineDashPattern:
//     [NSArray arrayWithObjects:[NSNumber numberWithInt:3],
//      [NSNumber numberWithInt:1],nil]];
//    
//    // Setup the path
//    CGMutablePathRef path = CGPathCreateMutable();
//
//    CGPathMoveToPoint(path, NULL, 0, 100);
//    CGPathAddLineToPoint(path, NULL,self.view.frame.size.width,100);
//
//    [shapeLayer setPath:path];
//    CGPathRelease(path);
//    
//    // 可以把self改成任何你想要的UIView, 下图演示就是放到UITableViewCell中的
//    [[self.tableV layer] addSublayer:shapeLayer];
//}
//
@end
